"""
商城接口
"""

from interface import bank_interface
from db import db_handler


def shopping_interface(username: str, shopping_cart: dict) -> tuple:
    """购物接口"""
    # 计算总金额
    total_prices = 0
    for order in shopping_cart.values():
        price, count = order
        total_prices += price*count

    res = bank_interface.pay_interface(username, total_prices)

    if res[0]:
        return True, '订单结算完成！'
    else:
        return False, res[1]


def add_shopping_cart_interface(username:str, shopping_cart: dict) -> tuple:
    """购物车添加商品接口"""

    user_dict = db_handler.select(username)
    # 从临时购物车获取商品名和数量
    for name, count in shopping_cart:
        
        # 判断该商品是否已经加入购物车，如果是就增加数量，否则添加商品
        if name in user_dict['shopping_cart']:
            user_dict['shopping_cart'][name][1] += count
        else:
            user_dict['shopping_cart'].update(shopping_cart)
    
    return True, '成功添加商品到购物车！'
